package other

import (
	"errors"
	"flag"
	"fmt"
	"sync"
	//"os"
)

var name string
var name1 string

//var name = flag.String("name", "1111", "are you name?")
var cmdLine = flag.NewFlagSet("question", flag.ExitOnError)
var cmdLine1 = flag.NewFlagSet("question1", flag.ExitOnError)

var a2 map[int]string

type operate func(x int, y int) int

var TestFace interface{}

type UserList struct {
	name string
	Course
	Grade
}

type Course struct {
	name string
	Grade
}

type Grade struct {
	code int
}

type TestType string

type Pet interface {
	transformArr(param [2][]string) [2][]string

}

type Pet1 interface {
	transformArr()
}

type Dog struct {
	name string
}

type PrtStruct struct {
	name string
}

var wait = sync.WaitGroup{}

// 任务调度
type Worker struct {
	Func func()
}

// 缓冲通道
var channels = make(chan Worker, 10)

// 任务协程池
func goPool(goroutineNum int)  {
	var wg sync.WaitGroup
	var taskNum = 1000000

	// 开启 n 个协程
	for i := 0; i < goroutineNum; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for ch := range channels {
				ch.Func()
			}
		}()
	}

	// 创建 n 个任务
	for i := 0; i < taskNum; i++ {
		j := i
		wk := Worker{
			Func: func() {
				j++
				fmt.Println(j)
			},
		}
		channels <- wk
	}
	// 关闭通道，并等待所有的msg完成
	close(channels)
	wg.Wait()
}

func init() {
	//flag.CommandLine = flag.NewFlagSet("", flag.PanicOnError)
	//flag.CommandLine.Usage = func() {
	//	fmt.Fprintf(os.Stderr, "Usage of %s\n:", "请务必输入昵称~")
	//}
	flag.StringVar(&name, "name", "make", "are you name?")
	flag.StringVar(&name, "name1", "make1", "are you name?")

}

// 将结构体作为指针关联, 修改p.name 也会修改原数据
func (p *PrtStruct) funcPointer( prt *int) {
	*prt += 10
	p.name = "李四"
}

func (data *UserList) getList() UserList {
	data.name = "王五"
	return *data
}

func (data Course) getList() Course {
	return data
}

func (data Grade) getList() Grade {
	return data
}

func (dog *Dog) transformArr(arr [2][]string) [2][]string {
	arr[0][0] = "0"
	return arr
}

func calculate2(x int, y int) func() int {
	return func() int {
		return x + y
	}
}

func calculate1(op operate) (operate, error) {
	if op == nil {
		return op, errors.New("参数错误")
	}

	return op, nil
}

func calculate(x int, y int, op operate) (int, error) {
	if op == nil {
		return 0, errors.New("参数错误")
	}

	return op(x, y), nil
}

func getIntChan() <-chan int {
	num := 5
	ch := make(chan int, num)
	for i := 0; i < num; i++ {
		ch <- i
	}
	close(ch)
	return ch
}

func channelSelect(ch1 chan int, ch chan<- int) {
	v, v1 := 0, 1
	for {
		select {
		case ch <- v:
			fmt.Println("通道发送", v)
		case <-ch1:
			fmt.Println("通道接收", v1)
			return
		}
	}
}

//func fibonacci(c, quit chan int) {
//	x, y := 0, 1
//	for {
//		select {
//		case c <- x:
//			x, y = y, x+y
//		case <-quit:
//			fmt.Println("quit")
//			return
//		}
//	}
//}

// 测试错误处理
func testError(str, y string) (val string, err error) {
	if y == "" {
		err = errors.New("内容不能为空")
		return
	}
	val = y
	return

}

func main() {
	//cmdLine.Parse(os.Args[1:])
	//cmdLine1.Parse(os.Args[1:])

	//flag.Parse()
	//fmt.Printf("Hello, %s\n", name)

	//container := []string{"1", "2", "3"}
	//container := interface{}(map[int]string{1: "张三"})
	//value, ok := interface{}(container).([]string)
	//value, ok := container.([]string)
	//fmt.Println(value, ok)
	//fmt.Println(string([]byte{'\xe4', '\xbd', '\xa0', '\xe5', '\xa5', '\xbd'}))
	//var srcInt = int16(-255)
	//dstInt := int8(srcInt)
	//fmt.Println(dstInt)
	//
	//type MyString string
	//type MyString1 =  string
	//type MyString2 []MyString
	//var testStr MyString = "hhh"
	//var testStr1 string = "12"
	//var testStr2 MyString1 = testStr1
	//var testStr3 MyString2 = []MyString{"1", "2"}
	//value1, ok1 := interface{}(testStr1).(string)
	//fmt.Println

	// 示例 1。
	//s1 := make([]int, 5)
	//fmt.Printf("The length of s1: %d\n", len(s1))
	//fmt.Printf("The capacity of s1: %d\n", cap(s1))
	//fmt.Printf("The value of s1: %d\n", s1)
	//s2 := make([]int, 5, 8)
	//fmt.Printf("The length of s2: %d\n", len(s2))
	//fmt.Printf("The capacity of s2: %d\n", cap(s2))
	//fmt.Printf("The value of s2: %d\n", s2)
	//a1 := map[string] interface{} {}
	//fmt.Println(a1, a2)

	//ch := make(chan int)
	//ch <- 100
	//val := <-ch
	//fmt.Println(val)

	//c := make(chan int)
	//go func() {
	//	for i := 0; i < 10; i = i + 1 {
	//		c <- i
	//	}
	//	close(c)
	//}()
	//for i := range c {
	//	fmt.Println(i)
	//}
	//fmt.Println("Finished")

	//c := make(chan int)
	//quit := make(chan int)
	//go func() {
	//
	//	quit <- 0
	//}()
	//fibonacci(c, quit)

	//ch := make(chan []int, 2)
	//t := []int {1, 2, 3}
	//go func() {
	//	fmt.Println(t)
	//	ch <- t
	//fmt.Println(<- ch)

	//close(ch)
	//}()
	//for  v := range ch {
	//	fmt.Println(v)
	//}
	//fmt.Println(<- ch)
	//fmt.Println(<- ch)
	//t[0] = 4
	//fmt.Println(<- ch)

	//ch := make(chan int)
	//ch1 := make(chan int)
	//
	//go func() {
	//	ch1 <- 12
	//}()
	//channelSelect(ch1, ch)
	//
	//way := getIntChan()
	//for el := range way {
	//	fmt.Println(el)
	//}

	//op := func(x int, y int) int {
	//	return x + y
	//}

	//val, err := calculate(1, 2, op)
	//add := calculate2(1, 2)
	//val := add()
	//fmt.Println(val)

	//arr := [2][] string{
	//	[]string{"1", "2"},
	//	[]string{"3", "4"},
	//}
	//fmt.Println(arr)
	//tarr := transformArr(arr)
	//fmt.Println(tarr)
	//fmt.Println(arr)

	//var users TestType
	//fmt.Println(users.getList())

	//grade1 := Grade{code: 1}
	//course := Course{name: "语文", Grade: grade1}
	//
	//users := UserList{
	//	name:   "张三",
	//	Course: course,
	//}
	//
	//fmt.Println(users.name, users.getList(), users.name, users.Course.getList(), users.Course.name, grade1, users.code, users.Grade, users.Course.Grade)

	//dog := Dog{name: "阿尔法"}
	//var pet Pet = &dog
	//fmt.Println(pet)
	//var dog *Dog
	//fmt.Println(dog)
	//dog1 := dog
	//fmt.Println(dog1)
	//var pet Pet = dog1
	//var pet Pet = nil
	//fmt.Println(pet, pet.transformArr(arr))

	//str := "1111"
	//str1 := str
	//str1 = "222"

	//map_test := map[int]int{
	//	1: 1,
	//	2: 2,
	//}

	//map_index := map_test[1]

	//int := 1
	//int1 := & int
	//int++
	//map_test[1]++
	//map_test[1]++
	//fmt.Println(int1, int1)

	// 定义指针变量
	//p := "我是指针变量"
	//// 传递指针并赋值给新的变量
	//p1 := &p
	//// 修改p1 也会修改 p
	//*p1 = "修改了值"
	//// 查看引用地址, p1 == p
	//fmt.Println(p1, &p, *p1, p, *p1 == p)


	//var pointer *int
	//var p *int
	//pointer_son := 10
	//pointer = &pointer_son
	//pointer1 := &pointer
	//pointer2 := &pointer1
	//fmt.Println(pointer, *pointer, *pointer1, **pointer1, ***pointer2, p)


	//val := 5
	//sval := PrtStruct{name:"张三"}
	//fmt.Println(val)
	//sval.funcPointer(&val)
	//fmt.Println(val, sval.name)

	//for i := 0; i < 5; i++ {
	//	go func() {
	//		fmt.Println(i)
	//	}()
	//}

	//channel := make(chan int)

	//wait.Add(1)
	//
	//go func() {
	//	//channel <- 1
	//	fmt.Println(1)
	//	wait.Done()
	//}()
	//
	//wait.Wait()

	//t := time.Now()
	//// 执行协程池
	//goPool(5)
	//elapsed := time.Since(t)
	//fmt.Println("总用时: ", elapsed)

	//slice := []int {1, 2, 3, 4, 5, 6}
	//sLen := len(slice) - 1
	//for i, e := range slice {
	//	if i == sLen {
	//		slice[0] += e
	//	} else {
	//		slice[i+1] += e
	//	}
	//}
	//fmt.Println(slice)

	arr := [2]string{"", "111"}

	for _, val := range arr {
		resp, err := testError(val, val)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(resp)
	}

}
